﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 羊只称重服务实现
    /// </summary>
    public class SheepWeightService : ISheepWeightService
    {
        /// <summary>
        /// 羊只称重仓储
        /// </summary>

        private readonly IBaseRepository<SheepWeight> sheepWeightRepository;

        /// <summary>
        /// 羊只档案仓储
        /// </summary>
        private readonly IBaseRepository<SheepRecord> sheepRecordRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sheepWeightRepository">羊只称重仓储</param>
        /// <param name="sheepRecordRepository">羊只档案仓储</param>
        public SheepWeightService(IBaseRepository<SheepWeight> sheepWeightRepository, IBaseRepository<SheepRecord> sheepRecordRepository)
        {
            this.sheepWeightRepository = sheepWeightRepository;
            this.sheepRecordRepository = sheepRecordRepository;
        }

        /// <summary>
        /// 添加羊只称重
        /// </summary>
        /// <param name="sheepWeight"></param>
        /// <returns></returns>
        public  async Task<int> AddSheepWeight(SheepWeight sheepWeight)
        {
            return sheepWeightRepository.Add(sheepWeight);      
        }

        /// <summary>
        /// 批量删除羊只称重
        /// </summary>
        /// <param name="sheepWeightIds"></param>
        /// <returns></returns>
        public int DelRangeSheepWeight(List<int> sheepWeightIds)
        {
            foreach (var item in sheepWeightIds)
            {
                var list = sheepWeightRepository.GetInfoBy(item);
                list.IsDel = 1;
                sheepWeightRepository.Update(list);
            }
            return 1;
        }

        /// <summary>
        /// 删除羊只称重
        /// </summary>
        /// <param name="sheepWeightId"></param>
        /// <returns></returns>
        public int DelSheepWeight(int sheepWeightId)
        {
            var list= sheepWeightRepository.GetValue(sheepWeightId); 
            list.IsDel = 1;
            return sheepWeightRepository.Update(list);  
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="sheepWeightId"></param>
        /// <returns></returns>
        public SheepWeight GetFtSheepWeight(int sheepWeightId)
        {
            return sheepWeightRepository.GetInfoBy(sheepWeightId);
        }
        /// <summary>
        /// 获取羊只称重列表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="overBit"></param>
        /// <param name="sheepType"></param>
        /// <param name="sheepSex"></param>
        /// <param name="minweightMonth"></param>
        /// <param name="maxweightMonth"></param>
        /// <param name="weightType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageOutput<SheepWeight>> GetSheepWeight(string? startDate, string? endDate, string? overBit, int sheepType, int sheepSex, int minweightMonth, int maxweightMonth, string? weightType, int pageIndex, int pageSize)
        {
            var list = sheepWeightRepository.GetAll().Where(c => c.IsDel == 0).ToList();
            //给我判断 开始时间和结束时间
            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                DateTime start = Convert.ToDateTime(startDate);
                DateTime end = Convert.ToDateTime(endDate);
                list = list.Where(c => c.WeightDate >= start && c.WeightDate <= end).ToList();
            }
            //给我判断耳号
            if (!string.IsNullOrEmpty(overBit))
            {
                list = list.Where(c => c.OverBit == overBit).ToList();
            }

            //给我判断 羊种
            if (sheepType > 0)
            {
                list = list.Where(c => c.SheepType == sheepType).ToList();
            }

            //给我判断 羊性别
            if (sheepSex>0)
            {
                bool   boolsheepSex = sheepSex == 1? true : false; 
                list = list.Where(c => c.SheepSex == boolsheepSex).ToList();
            }
            //给我判断称重类型
            if (!string.IsNullOrEmpty(weightType))
            {
                list = list.Where(c => c.WeightType == weightType).ToList();
            }
            //给我判断最大最小月龄
            if (minweightMonth > 0)
            {
                list = list.Where(c => c.WeightMonth >= minweightMonth).ToList();
            }
            if (maxweightMonth > 0)
            {
                list = list.Where(c => c.WeightMonth < maxweightMonth).ToList();
            }

            var result = new PageOutput<SheepWeight>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.SheepWeightId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;

            return result;

        }

        /// <summary>
        /// 更新羊只称重
        /// </summary>
        /// <param name="sheepWeight"></param>
        /// <returns></returns>
        public async Task<int> UpdaSheepWeight(SheepWeight sheepWeight)
        {
            return sheepWeightRepository.Update(sheepWeight);
        }
    }
}
